package com.fruit.controller.app.rights;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.goods.GoodsSpecTimeHarvest;
import com.fruit.pojo.goods.RightConfig;
import com.fruit.pojo.rights.UserGoodsRights;
import com.fruit.pojo.rights.UserGoodsRightsPost;
import com.fruit.service.goods.GoodsManager;
import com.fruit.service.rights.UserRightsManager;
import com.fruit.util.DateUtil;
import com.fruit.util.ResultInfo;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 
 * @ClassName: UserGoodsRightControllerApp
 * @author 葛欢欢
 * @version 2017.11.30 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/app/user/rights")
public class UserGoodsRightControllerApp extends BaseController {

	@Autowired
	private UserRightsManager userRightsManager;

	@Autowired
	private GoodsManager goodsManager;

	/**
	 * 权益首页
	 * 
	 * @param userId
	 *            userId
	 * @param page
	 *            page
	 * @param showNum
	 *            showNum
	 * @param request
	 *            request
	 * @param session
	 *            session
	 * @return Map<String, Object>
	 * @author 葛欢欢
	 * @version 2017.11.30 12:20
	 */
	@RequestMapping(value = "/index", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> index(String userId, int page, int showNum, HttpServletRequest request,
			HttpSession session) {
		long a = System.currentTimeMillis();
		log.info("【获取权益首页进入时间】" + DateUtil.getTime());
		log.info("【获取权益首页】---【开始】---");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> pam = new HashMap<String, Object>();
		pam.put("userId", userId);
		pam.put("start", (page - 1) * showNum);
		pam.put("num", showNum);
		try {
			// 查询每个大类的统计百分比
			List<RightConfig> rightConfig = userRightsManager.getClassPercentage(Integer.parseInt(userId));
			// 权益列表
			List<RightConfig> userGoodsRightsList = userRightsManager.getGoodsRightList(pam);
			resultMap.put("rightConfig", rightConfig);
			resultMap.put("goodsList", userGoodsRightsList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			log.info("【获取权益首页】---【查询错误】---");
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}

		log.info("【获取权益首页】---【结束】---");
		log.info("【获取权益首页耗时】" + (System.currentTimeMillis() - a) / 1000f + "秒");
		return resultMap;
	}
	
	/**
	 * 权益首页
	 * 
	 * @param userId
	 *            userId
	 * @param page
	 *            page
	 * @param showNum
	 *            showNum
	 * @param request
	 *            request
	 * @param session
	 *            session
	 * @return Map<String, Object>
	 * @author 葛欢欢
	 * @version 2017.11.30 12:20
	 */
	@RequestMapping(value = "/myOrder", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> myOrder(String userId, int page, int showNum, HttpServletRequest request,
			HttpSession session) {
		long a = System.currentTimeMillis();
		log.info("【获取权益首页进入时间】" + DateUtil.getTime());
		log.info("【获取权益首页】---【开始】---");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> pam = new HashMap<String, Object>();
		pam.put("userId", userId);
		pam.put("start", (page - 1) * showNum);
		pam.put("num", showNum);
		try {
			// 权益列表
			List<RightConfig> userGoodsRightsList = userRightsManager.getGoodsRightList(pam);
			resultMap.put("goodsList", userGoodsRightsList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			log.info("【获取权益首页】---【查询错误】---");
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}

		log.info("【获取权益首页】---【结束】---");
		log.info("【获取权益首页耗时】" + (System.currentTimeMillis() - a) / 1000f + "秒");
		return resultMap;
	}

	/**
	 * 获取用户权益列表
	 * 
	 * @param userId
	 *            userId
	 * @param classId
	 *            classId
	 * @param page
	 *            page
	 * @param showNum
	 *            showNum
	 * @param request
	 *            request
	 * @return Map<String, Object>
	 * @author 葛欢欢
	 * @version 2017.11.30 12:20
	 */
	@RequestMapping(value = "/fruit", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> fruit(String userId, String classId, int page, int showNum, HttpServletRequest request) {
		long a = System.currentTimeMillis();
		log.info("【获取权益列表进入时间】" + DateUtil.getTime());
		log.info("【获取权益列表】---【开始】---");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> pam = new HashMap<String, Object>();
		String classString = null;
		if(StringUtils.isNotBlank(classId)){
			classString = goodsManager.getClassIdByPid(classId);
		}
		pam.put("uId", userId); // 用户id
		pam.put("classId", classString); // 总的分类id
		pam.put("start", (page - 1) * showNum);
		pam.put("num", showNum);
		try {
			// 获取权益列表
			List<UserGoodsRights> userGoodsRights = userRightsManager.userTrees(pam); // 获取权益列表
			if (userGoodsRights == null) {
				resultMap.put("result", this.initResult(ResultInfo.ERROR, "no", "请购买"));
			}

			resultMap.put("userGoodsRights", userGoodsRights);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();

			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【获取权益列表】---【结束】---");
		log.info("【获取权益列表耗时】" + (System.currentTimeMillis() - a) / 1000f + "秒");
		return resultMap;
	}

	/**
	 * 通过批次ID 获取权益列表
	 * 
	 * @param userId
	 * @param objId
	 * @param page
	 * @param showNum
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getRightsBatch", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getRightsBatch(Integer userId, Integer objId, int page, int showNum,
			HttpServletRequest request) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		log.info("【获取批次权益列表进入时间】-【批次ID:{}】-【开始】", objId);
		if (userId == null || objId == null) {
			resMap.put("result", this.initResult(ResultInfo.ERROR, "params", "参数错误"));
			return resMap;
		}

		GoodsSpecTimeHarvest gst = goodsManager.getGoodsHarvestById(objId);
		if (gst == null) {
			resMap.put("result", this.initResult(ResultInfo.ERROR, "params", "参数错误"));
			return resMap;
		}
		Map<String, Object> pam = new HashMap<String, Object>();
		pam.put("gstId", gst.getGoodsTimeSpecId());
		pam.put("uId", userId); // 用户id
		pam.put("start", (page - 1) * showNum);
		pam.put("num", showNum);
		try {
			// 获取权益列表
			List<UserGoodsRights> userGoodsRights = userRightsManager.getRightsByGstId(pam);
			if (userGoodsRights == null) {
				resMap.put("result", this.initResult(ResultInfo.ERROR, "no", "请购买"));
			}

			resMap.put("userGoodsRights", userGoodsRights);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}

		return resMap;

	}

	/**
	 * 获取消息
	 * 
	 * @param request
	 *            request
	 * @param session
	 *            session
	 * @return Map<String, Object>
	 * @author 葛欢欢
	 * @version 2017.11.30 12:20
	 */
	@RequestMapping(value = "/getNews", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getNews(HttpServletRequest request, HttpSession session) {
		long a = System.currentTimeMillis();
		log.info("【获取消息进入时间】" + DateUtil.getTime());
		log.info("【获取消息】---【开始】---");
		Map<String, Object> resMap = new HashMap<>();
		try {
			// 类型 00 权益消息气泡 01 首页消息气泡 02 钱包首页轮询消息
			List<UserGoodsRightsPost> bulletinList = userRightsManager.findUserGoodsRightsPost("02");
			resMap.put("bulletinList", bulletinList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
			log.info("【获取消息返回值bulletinList：】 " + bulletinList);
		} catch (Exception e) {
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【获取消息】---【结束】---");
		log.info("【获取消息耗时】" + (System.currentTimeMillis() - a) / 1000f + "秒");
		return resMap;
	}

}
